package com.inspinia.upms.rpc.util;


import com.inspinia.upms.common.model.resource.UpmsResource;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 把一个list集合,里面的bean含有 parentId 转为树形式
 *
 */
public class TreeUtil {
	
	
	/**
	 * 根据父节点的ID获取所有子节点
	 * @param list 分类表
	 * @param praentId 传入的父节点ID
	 * @return String
	 */
	public List<UpmsResource> getChildTreeObjects(List<UpmsResource> list, Long praentId) {
		List<UpmsResource> returnList = new ArrayList<UpmsResource>();
		for (Iterator<UpmsResource> iterator = list.iterator(); iterator.hasNext();) {
			UpmsResource t = (UpmsResource) iterator.next();
			// 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
			if (t.getPid().equals(praentId)) {
				recursionFn(list, t,"");
				returnList.add(t);
			}
		}
		return returnList;
	}
	
	/**
	 * 递归列表
	 * @author lanyuan
	 * Email: mmm333zzz520@163.com
	 * @date 2013-12-4 下午7:27:30
	 * @param list
	 * @param t
	 */
	private void  recursionFn(List<UpmsResource> list, UpmsResource t) {
		// 得到子节点列表
		List<UpmsResource> childList = getChildList(list, t);
		for (UpmsResource tChild : childList) {
			// 判断是否有子节点
			if (hasChild(list, tChild)) {
				Iterator<UpmsResource> it = childList.iterator();
				while (it.hasNext()) {
					UpmsResource n = (UpmsResource) it.next();
					recursionFn(list, n);
				}
			}
		}
	}

    /**
     * 得到子节点列表
	 * @param list
     * @param t
     * @return
     */
	private List<UpmsResource> getChildList(List<UpmsResource> list, UpmsResource t) {
		
		List<UpmsResource> tlist = new ArrayList<UpmsResource>();
		Iterator<UpmsResource> it = list.iterator();
		while (it.hasNext()) {
			UpmsResource n = (UpmsResource) it.next();
			if (n.getPid().equals(t.getId())) {
				tlist.add(n);
			}
		}
		return tlist;
	} 
	List<UpmsResource> returnList = new ArrayList<UpmsResource>();

	/**
     * 根据父节点的ID获取所有子节点
     * @param list 分类表
     * @param typeId 传入的父节点ID
     * @param prefix 子节点前缀
     */
    public List<UpmsResource> getChildTreeObjects(List<UpmsResource> list, int typeId,String prefix){
        if(list == null) return null;
        for (Iterator<UpmsResource> iterator = list.iterator(); iterator.hasNext();) {
			UpmsResource node = (UpmsResource) iterator.next();
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (node.getPid().equals(typeId)) {
                recursionFn(list, node,prefix);
            }
        }
        return returnList;
    }
     
    private void recursionFn(List<UpmsResource> list, UpmsResource node,String p) {
		// 得到子节点列表
        List<UpmsResource> childList = getChildList(list, node);
		// 判断是否有子节点
        if (hasChild(list, node)) {
            returnList.add(node);
            Iterator<UpmsResource> it = childList.iterator();
            while (it.hasNext()) {
				UpmsResource n = (UpmsResource) it.next();
                n.setName(p + n.getName());
                recursionFn(list, n,p + p);
            }
        } else {
            returnList.add(node);
        }
    }

	/**
     * 判断是否有子节点
	 * @param list
     * @param t
     * @return
     */
	private boolean hasChild(List<UpmsResource> list, UpmsResource t) {
		return getChildList(list, t).size() > 0 ? true : false;
	}

}
